import 'dart:convert';

import 'package:emby/model/api_response.dart';
import 'package:emby/util/function.dart';
import 'package:flutter/material.dart';
import 'package:cool_ui/cool_ui.dart';

class AddServer extends StatefulWidget {
  @override
  _AddServerState createState() => _AddServerState();
}

class _AddServerState extends State<AddServer> {
  String name;
  String host;
  String port = '8096';
  TextEditingController _nameController = TextEditingController();
  TextEditingController _hostController = TextEditingController();
  TextEditingController _portController = TextEditingController();
  @override
  void initState() {
    _portController.value = TextEditingValue(text: port, selection: TextSelection.collapsed(offset: port.length));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("添加服务器"),
      ),
      body: ListView(
        padding: EdgeInsets.symmetric(horizontal: 10),
        children: <Widget>[
          TextField(
            decoration: InputDecoration(
              hintText: "请填写服务器名",
              labelText: "服务器名",
              helperText: "方便区分多个服务器，可随意填写",
            ),
            onChanged: (v) => name = v,
          ),
          TextField(
            decoration: InputDecoration(
              hintText: "主机",
              labelText: "请填写主机地址",
              helperText: "如192.168.1.100或http://domain.com",
            ),
            onChanged: (v) => host = v,
          ),
          TextField(
            controller: _portController,
            decoration: InputDecoration(
              hintText: "请填写端口号",
              labelText: "端口号",
              helperText: "默认端口号为8096",
            ),
            onChanged: (v) => port = v,
          ),
          SizedBox(
            height: 20,
          ),
          FlatButton(
            color: Theme.of(context).primaryColor,
            child: Text(
              "连接",
              style: TextStyle(color: Colors.white),
            ),
            onPressed: () async {
              ApiResponse res = await Api.userPublic(host: host, port: port);
              if (res.code == 1) {
                //存储当前服务器
                //获取保存的服务器
                List servers = [];
                String serverStr = await Util.getStorage("services");
                if (serverStr != null && serverStr != "") {
                  servers = json.decode(serverStr);
                } else {
                  servers = [];
                }
                servers.add({
                  "name": name,
                  "host": host,
                  "port": port,
                });
                await Util.setStorage("services", json.encode(servers));
                await Util.setStorage("host", "$host:$port/");
                Util.setBaseUrl("$host:$port/");
                Navigator.of(context).pushNamedAndRemoveUntil("/loginPage", (route) => false);
              }
            },
          ),
        ],
      ),
    );
  }
}
